package com.wyj.controller;

import com.wyj.event.EventProducer;
import com.wyj.pojo.Page;
import com.wyj.pojo.User;
import com.wyj.pojo.event;
import com.wyj.service.imp.FollowServiceImp;
import com.wyj.service.imp.UserServiceImp;
import com.wyj.util.CommunityConstant;
import com.wyj.util.CommunityUtil;
import com.wyj.util.HostHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * @author 王永杰
 * @Description 描述该类的做什么用
 * @since 2022/7/28
 */
@Controller
public class FollowController implements CommunityConstant {

    @Autowired
    FollowServiceImp followServiceImp;

    @Autowired
    HostHolder hostHolder;

    @Autowired
    UserServiceImp userServiceImp;

    @Autowired
    EventProducer eventProducer;

    //关注的时候
    @RequestMapping(value = "/follow",method = RequestMethod.POST)
    @ResponseBody
    public String follow(int entityType, int entityId) {


        //当前用户
        User user = hostHolder.getUser();
        followServiceImp.followee(user.getId(),entityType,entityId);


        event event = new event();
        event.setTopic(TOPIC_FOLLOW);
        event.setUserId(user.getId());
        event.setEntityType(entityType);
        event.setEntityId(entityId);
        event.setEntityUserId(entityId);

        eventProducer.fireEvent(event);


        return CommunityUtil.getJsonString(0,"已关注");
    }

    //取关
    @RequestMapping(value = "/unfollow",method = RequestMethod.POST)
    @ResponseBody
    public String unfollow(int entityType,int entityId) {

        //当前用户
        User user = hostHolder.getUser();
        followServiceImp.unfollowee(user.getId(), entityType, entityId);
        return CommunityUtil.getJsonString(0, "已取关");
    }

    //显示出某个用户的所有的关注
    @RequestMapping(value = "/getFollowees/{userId}",method = RequestMethod.GET)
    public String getFollowers(@PathVariable("userId") int userId, Model model, Page page) {

        //设置分页
        page.setLimit(8);
        page.setPath("/getFollowes/"+userId);
        page.setRows(followServiceImp.focus(userId,3));

        List<Map<String, Object>> list = followServiceImp.getFollowees(userId, 3,page.getOffset(),page.getLimit());

        //这是某个用户主页的 确定 该用户
        User user = userServiceImp.getUserById(userId);

        model.addAttribute("user",user);
        model.addAttribute("list",list);

        return "site/followee";
    }

    //显示出某个用户的所有的粉丝
    @RequestMapping(value = "/getFollowers/{userId}",method = RequestMethod.GET)
    public String getFollowere(@PathVariable("userId") int userId,Model model,Page page) {

        //设置分页
        page.setLimit(8);
        page.setPath("/getFollowers/"+userId);
        page.setRows(followServiceImp.followers(userId,3));

        List<Map<String, Object>> list = followServiceImp.getFollowers(3, userId,page.getOffset(),page.getLimit());

        //这是某个用户主页的 确定 该用户
        User user = userServiceImp.getUserById(userId);

        model.addAttribute("user",user);
        model.addAttribute("list",list);


        return "site/follower";
    }






}
